package JZ62_二叉搜索树的第k个结点;

import java.util.ArrayList;

class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}

public class Solution {
    ArrayList<TreeNode> list = new ArrayList<>();

    TreeNode KthNode(TreeNode pRoot, int k) {
            inorder(pRoot);

            if (k >= 1 && list.size() >=k){
                return list.get(k - 1);
            }

            return null;

    }

    private void inorder(TreeNode cur) {
        if (cur != null){
            inorder(cur.left);
            list.add(cur);
            inorder(cur.right);
        }
    }
}